package org.example;


import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;


//用于监听服务上下线的变更事件 用于增量维护服务列表
@RestController
public class ListenerController {

    @Autowired
    private  ServiceContext serviceContext;

    @PostMapping("/notifyLogin")
    public void notify(@RequestBody ServiceChangeEvent event) {
        if(ChangeType.LOGIN != event.getChangeType()){
            System.out.println(event.getChangeType());
            throw new RuntimeException("参数错误");
        }
        ServiceInstance serviceInstance = event.getServiceInstance();
        serviceContext.login(serviceInstance);

    }

    @PostMapping("/notifyLogout")
    public void notifyLogout(@RequestBody ServiceChangeEvent event) {
        if(ChangeType.LOGOUT!= event.getChangeType()){
            throw new RuntimeException("参数错误");
        }
        ServiceInstance serviceInstance = event.getServiceInstance();
        serviceContext.logout(serviceInstance);
    }
}
